function addHome(){
	var vDomainName='http://'+document.domain+'/';
    if (window.sidebar)
    {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        }
        catch (e)
        {
            alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将[signed.applets.codebase_principal_support]设置为true");
        }

        var prefs =  Components.classes["@mozilla.org/preferences-service;1"].getService( Components.interfaces.nsIPrefBranch );
        prefs.setCharPref("browser.startup.homepage",vDomainName);
    }
    else if(document.all)
    {
        document.body.style.behavior="url(#default#homepage)";
        document.body.setHomePage(vDomainName);
    }
    else
    {
        ;
    }
}
function addFavorite(){
	var sURL='',sTitle='';
	sURL=window.location;
	sTitle=document.title;
	try{
		window.external.addFavorite(sURL, sTitle);
	}
	catch(e){
		try{
			window.sidebar.addPanel(sTitle, sURL, "");
		}
		catch (e){
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
	}
}
function addMyStock(){
	if(code){
		var myStock = $.cookie('myStock');
		if(myStock == null) myStock = "";
		var myStockArr = myStock.split(";"), newMyStock = code;
		for(var i = 0; i< myStockArr.length && i <7 ; i++){
			if(myStockArr[i] == code) continue;
			newMyStock += (";" + myStockArr[i]);
		}
		$.cookie('myStock', newMyStock, {expires: 7, path: '/'});
		freshMyStock();
	}
}
function freshMyStock(){
	var myStock = $.cookie('myStock');
	if(myStock != null){
		$.ajax({
	        url: 'stockHq.php',
	        cache: false,
	        data: "code="+myStock,
	        dataType: 'json',
	        success: function(data){
	        	if(data){
					var html = '';
					$.each(data, function(index, entry){
						var cla = (Number(entry.zd) >= 0 ? "red" : "green");
						var name = entry.name.substring(0, 4);
						html += ('<dd><a class="stockname" href="SearchStock.php?code='+entry.code+'">'+name+'</a><span class="price '+cla+'">'+entry.lp+'</span><span class="swing '+cla+'">'+entry.zf+'</span></dd>');
					});
					$('#myStock').html('<dd><span class="stockname">股票名称</span><span class="price">价格(元)</span><span class="swing">涨跌幅</span></dd>');
					$('#myStock').append(html);;
				}
	        }
	   });
	}
}
function freshViewHistory(){
	var stockHistory = $.cookie('stockHistory');
	if(stockHistory == null) stockHistory = "";
	var historyArr = stockHistory.split(";"), newStockHistory = code;
	for(var i = 0; i< historyArr.length && i <8 ; i++){
		if(historyArr[i] == code) continue;
		newStockHistory += (";" + historyArr[i]);
	}
	$.cookie('stockHistory', newStockHistory, {expires: 7, path: '/'});
	if(newStockHistory != code){
		$.ajax({
	        url: 'stockHq.php',
	        cache: false,
	        data: "code="+newStockHistory,
	        dataType: 'json',
	        success: function(data){
	        	if(data){
					var html = '';
					$.each(data, function(index, entry){
						var cla = (Number(entry.zd) >= 0 ? "red" : "green");
						var name = entry.name.substring(0, 4);
						html += ('<dd><a class="stockname" href="SearchStock.php?code='+entry.code+'">'+name+'</a><span class="price '+cla+'">'+entry.lp+'</span><span class="swing '+cla+'">'+entry.zf+'</span></dd>');
					});
					$('#viewHis').append(html);;
				}
	        }
	   });
	}
}
String.prototype.length2 = function() {
    var cArr = this.match(/[^\x00-\xff]/ig);
    return this.length + (cArr == null ? 0 : cArr.length);
}

$(document).ready(function(){
	(function($){
		$('.header .nav').find('a').addClass(function(){
			var strLength=Math.floor($(this).get(0).innerHTML.length2()/2);
			return 'f'+(strLength>8?8:(strLength<4?4:strLength));
		});
	})(jQuery);
	
	(function($){
		$('.footer .ft_menu').find('li:first').addClass('first').end().find('li').has('ul').addClass('parent').hover(
		function () {
			$(this).addClass('active').find('> ul').show();
		},
		function () {
			$(this).removeClass('active').find('> ul').hide();
		})
	})(jQuery);
	
	(function($){
		$('.tabs_all').find('ul.tabs').each(function(){
			if(!$('li',this).hasClass("active")) $('li:first',this).addClass('active').parent('ul.tabs').siblings('.panels').find('> div.panels_box').hide().eq(0).show();
			$('li',this).each(function(index){
				var currentObj=$(this);
				currentObj.find('a').andSelf().css({'z-index':(50-index)});
				currentObj.bind('click hover',function(){
					currentObj.siblings('li').removeClass("active").end().addClass("active");
					if(currentObj.parent('ul').attr('change') && currentObj.parent('ul').attr('change')=='false'){
						return false;
					}
					else{
						currentObj.parent('ul.tabs').siblings('.panels').find('> div.panels_box').hide().eq(index).show();
						return false;
					}
				});
			});
		});
	})(jQuery);
	
	(function($){
		$('.tablehover').each(function(){
			$(this).find('tr:not(.nohover)').hover(
				function(){
					$(this).addClass("hover");
				},
				function(){
					$(this).removeClass("hover");
				}
			)
		});
	})(jQuery);
	
		
	$('.news_tag a').click(function(event){
		event.preventDefault();
		switch($(this).attr('class')){
			case 'big': $('.news_Txt').css({'font-size':'16px','line-height':'25px'});
				break;
			case 'medium': $('.news_Txt').css({'font-size':'14px','line-height':'22px'});
				break;
			case 'small': $('.news_Txt').css({'font-size':'12px','line-height':'20px'});
				break;
			default:
		}
	});

	if($('.tdfixed').get(0)){
		$('.tdfixed').each(function(){
			var $fixedObj=$(this);
			$fixedObj.attr('tooltips',$fixedObj.html()).parent().css({'width':$(this).width(),'height':$(this).height()})
		}).tipTip({axWidth:"162px",attribute:'tooltips',defaultPosition:'top',activation:'click'}).bind('focus mouseover',function(){
			$(this).trigger("click");
		});
	}
	
	$(function(){
		if($('.topads').length>0){	
			var $block = $('.topads'), 
				$ad = $block.find('a'),
				showIndex = 0,			// 預設要先顯示那一張
				fadeOutSpeed = 800,		// 淡出的速度
				fadeInSpeed = 800,		// 淡入的速度
				defaultZ = 2,			// 預設的 z-index
				isHover = false,
				timer, speed = 3500;	// 計時器及輪播切換的速度
			
			$ad.css({opacity: 0,zIndex: defaultZ - 1}).eq(showIndex).css({opacity: 1,zIndex: defaultZ});
	
			var str = '';
			for(var i=0;i<$ad.length;i++){str += '<a href="#">' + (i + 1) + '</a>';}
			var $controlA = $('.topads').append($('<div class="control">' + str + '</div>').css('zIndex', defaultZ + 1)).find('.control a');
			$controlA.click(function(){
				showIndex = $(this).text() * 1 - 1;
				$ad.eq(showIndex).stop().fadeTo(fadeInSpeed, 1, function(){
					if(!isHover){timer = setTimeout(autoClick, speed);}
				}).css('zIndex', defaultZ).siblings('a').stop().fadeTo(fadeOutSpeed, 0).css('zIndex', defaultZ - 1);
				$(this).addClass('on').siblings().removeClass('on');
	
				return false;
			}).focus(function(){
				$(this).blur();
			}).eq(showIndex).addClass('on');
	
			$block.hover(function(){
				isHover = true;
				clearTimeout(timer);
			}, function(){
				isHover = false;
				timer = setTimeout(autoClick, speed);
			})
			
			function autoClick(){
				if(isHover) return;
				showIndex = (showIndex + 1) % $controlA.length;
				$controlA.eq(showIndex).click();
			}
			timer = setTimeout(autoClick, speed);
		}
	});
});